<?php

// This class is responsible for building the list of
// comments and discussions needing moderating.
class ModerationList extends PostBackControl{

   // Variables used to display discussions needing moderation.
   var $DiscussionData;
   var $DiscussionPageList;
   var $DiscussionPageDetails;
   var $DiscussionsFound;

   // Variables used to display comments needing moderation.
   var $CommentData;
   var $CommentPageList;
   var $CommentPageDetails;
   var $CommentsFound;

   // The list of Role IDs with the auto-hide 'permission'.
   public static $RoleIDs = array();

   // Variables required by the templates, but not otherwise used.
   var $Search;
   var $DataCount;
   var $PageList;
   var $PageDetails;

   // Construct the control with safe initial values.
   function ModerationList(&$Context){
      $this->Name = 'ModerationList';
      $this->ValidActions = array('AutoHideList');
      $this->DiscussionData = '';
      $this->DiscussionsFound = 0;
      $this->DiscussionPageList = '';
      $this->DiscussionPageDetails = $Context->GetDefinition('NoSearchResultsMessage');
      $this->CommentData = '';
      $this->CommentsFound = 0;
      $this->CommentPageList = '';
      $this->CommentPageDetails = $Context->GetDefinition('NoSearchResultsMessage');
      $this->DataCount = 0;
      $this->PageList = '';
      $this->PageDetails = '';
      $this->Constructor($Context);
      }

   // This function queries the database for Auto-Hidden comments and
   // discussions, and fills the appropriate instance variables used 
   // for displaying the results.
   function PopulateList(){
      
      // Very little to do if we don't have any roles with auto-hide turned on.
      if (count(ModerationList::$RoleIDs) > 0){
         
	 // Pull the list of auto-hidden discussions.
         $DiscussionManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 
      		'DiscussionManager');

         // We already have a delegate attached to the SqlBuilder in this case,
         // so just run the search.
         $this->DiscussionData = $DiscussionManager->GetDiscussionSearch(0,'1',
		$this->Context->ObjectFactory->NewContextObject($this->Context, 'Search'));
         $this->DiscussionsFound = $this->Context->Database->RowCount($this->DiscussionData);
         if ($this->DiscussionsFound > 0){
            $this->DiscussionPageDetails = "Discussions 1 to " . $this->DiscussionsFound 
	    	. " needing moderation.";
         }

         // Pull the list of auto-hidden comments.
         $CommentManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 
      		'CommentManager');

         // CommentManager doesn't have a delegate on the sql-builder for search.  We have 
         // to use the search-builder directly.
         $SearchQuery = $CommentManager->GetSearchBuilder(
		$this->Context->ObjectFactory->NewContextObject($this->Context,'Search'));
         $SearchQuery->AddWhere('c','Deleted','','1','=','and');
         $RoleIDs = ModerationList::$RoleIDs;
         $FirstRoleID = array_shift($RoleIDs);
         if (count($RoleIDs) > 0){
            $SearchQuery->AddWhere('a','RoleID','',$FirstRoleID,'=','and','','1','1');
            foreach ($RoleIDs as $RoleID){
               $SearchQuery->AddWhere('a','RoleID','',$RoleID,'=','or');
            }
            $SearchQuery->EndWhereGroup();
         }
         else{
            $SearchQuery->AddWhere('a','RoleID','',$FirstRoleID,'=','and');
         }
         $this->CommentData = $this->Context->Database->Select($SearchQuery,'ModerationList',
		'PopulateList',
		$this->Context->GetDefinition('AutoHide_ErrorGettingCommentsForModeration'));
         $this->CommentsFound = $this->Context->Database->RowCount($this->CommentData);
         if ($this->CommentsFound > 0){
            $this->CommentPageDetails = "Comments 1 to " . $this->CommentsFound 
	    	. " needing moderation.";
         }
      }
      $this->DiscussionPageList = $this->GetPageList($this->DiscussionsFound);
      $this->CommentPageList = $this->GetPageList($this->CommentsFound);
   }

   // This function creates a PageList object for the search results templates, where
   // all results appear on one page.
   function GetPageList($RecordCount){
      $PageList = $this->Context->ObjectFactory->NewContextObject($this->Context,
      	'PageList');
      $PageList->TotalRecords = ($RecordCount == 0) ? 1 : $RecordCount;
      $PageList->CssClass = 'PageList';
      $PageList->CurrentPage = '1';
      $PageList->RecordsPerPage = ($RecordCount == 0) ? 1: $RecordCount;
      $PageList->PagesToDisplay = 1;
      return $PageList->GetLiteralList();
   }

   function Render_NoPostBack(){
      // Two things to render...discussion search results and comment results.
      // Basically, include the search results templates twice.
      if ($this->PostBackAction == 'AutoHideList'){

         // Get the paths to the search templates.
	 $SearchTemplateTop = ThemeFilePath($this->Context->Configuration, 
	 	'search_results_top.php');
	 $SearchTemplateBottom = ThemeFilePath($this->Context->Configuration, 
	 	'search_results_bottom.php');

         // Render the auto-hidden-discusssions list.
	 $this->Search = $this->Context->ObjectFactory->NewContextObject($this->Context,'Search');
	 $this->Search->Type = '';
	 $this->DataCount = $this->DiscussionsFound;
	 $this->PageList = $this->DiscussionPageList;
	 $this->PageDetails = $this->DiscussionPageDetails;
	 $SearchID = 'Discussions';

	 // Have to match the "#SearchPage .ContentInfo h1" selector to get the 
	 // heading style to mirror the search pages.
	 echo '<div id="SearchPage"><div class="ContentInfo">';
	 echo '   <h1>'. $this->Context->GetDefinition('AutoHide_DiscussionModerationHeading') 
	 	. '</h1></div>';
	 include($SearchTemplateTop);
	 $CurrentUserJumpToLastCommentPref = $this->Context->Session->User->Preference(
	 	'JumpToLastReadComment');
	 if ($this->DiscussionsFound > 0){
	    $DiscussionTemplate = ThemeFilePath($this->Context->Configuration, 'discussion.php');
	    $FirstRow = 1;
	    $Alternate = 0;
	    $Discussion = $this->Context->ObjectFactory->NewContextObject($this->Context,
	    	'Discussion');
	    $DiscussionList = '';
	    while ($Row = $this->Context->Database->GetRow($this->DiscussionData)){
	       $Discussion->Clear();
	       $Discussion->GetPropertiesFromDataSet($Row, $this->Context->Configuration);
	       $Discussion->FormatPropertiesForDisplay();
	       include($DiscussionTemplate);
	       $FirstRow = 0;
	       $Alternate = FlipBool($Alternate);
	    }
	    echo($DiscussionList);
	 }
	 include($SearchTemplateBottom);
	 // Close off the SearchPage div.
	 echo '</div>';

	 // Render the auto-hidden-comments list.
         $this->Search->Type = '';
         $this->DataCount = $this->CommentsFound;
         $this->PageList = $this->CommentPageList;
         $this->PageDetails = $this->CommentPageDetails;
	 $SearchID = 'CommentResults';

	 // Have to match the "#SearchPage .ContentInfo h1" selector to get the 
	 // heading style to mirror the search pages.
	 echo '<div id="SearchPage"><div class="ContentInfo">';
	 echo '   <h1>' . $this->Context->GetDefinition('AutoHide_CommentModerationHeading') 
	 	. '</h1></div>';
         include($SearchTemplateTop);
	 if ($this->CommentsFound > 0){
	    $CommentTemplate = ThemeFilePath($this->Context->Configuration, 'search_results_comments.php');
	    $Comment = $this->Context->ObjectFactory->NewContextObject($this->Context,'Comment');
	    $HighlightWords = array();
	    $CommentList = '';
	    $FirstRow = 1;
	    $Alternate = 0;
	    while ($Row = $this->Context->Database->GetRow($this->CommentData)){
	       $Comment->Clear();
	       $Comment->GetPropertiesFromDataSet($Row, $this->Context->Configuration);
	       $Comment->FormatPropertiesForDisplay();
	       include($CommentTemplate);
	       $FirstRow = 0;
	       $Alternate = FlipBool($Alternate);
	    }
	    echo($CommentList);
	 }
         include($SearchTemplateBottom);
	 // Close off the SearchPage div.
	 echo '</div>';
      }
   }
}

?>
